﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.IO;

namespace Qplus.UI
{
    public class ImageProcessing
    {
        public static byte[] ImageToByteArray(BitmapImage imageSource)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                WriteableBitmap btmMap = new WriteableBitmap(imageSource);
                // write an image into the stream
                Extensions.SaveJpeg(btmMap, ms,
                    imageSource.PixelWidth, imageSource.PixelHeight, 0, 100);
                return ms.ToArray();
            }
        }
        public static BitmapImage ByteArrayToBitmapImage(byte[] bits)
        {
            BitmapImage bitmapImage = new BitmapImage();
            using (MemoryStream ms = new MemoryStream(bits))
            {
                bitmapImage.CreateOptions = BitmapCreateOptions.DelayCreation;
                bitmapImage.SetSource(ms);
                return bitmapImage;
                //Image image = new Image();
                //image.Source = bitmapImage;
                //return image;
            }
        }
        public static BitmapImage ByteArrayToBitmapImage(byte[] bits, int index, int length)
        {
            BitmapImage bitmapImage = new BitmapImage();
            MemoryStream ms = new MemoryStream();
            ms.Write(bits, index, length);
            bitmapImage.SetSource(ms);
            return bitmapImage;
            //Image img = new Image();
            //img.Source = bitmapImage;
            //return img;
        }
        /// <summary>
        /// 获取缩略图
        /// </summary>
        /// <param name="source">原图像流</param>
        /// <param name="width">缩略图宽</param>
        /// <param name="height">缩略图高</param>
        /// <returns>缩略图流</returns>
        public static Stream GetThumbImage(Stream source, int width, int height)
        {
            if (source == null)
                return null;

            Stream target = new MemoryStream();
            BitmapImage bitmap = new BitmapImage();
            bitmap.CreateOptions = BitmapCreateOptions.None;
            bitmap.SetSource(source);
            WriteableBitmap wb = new WriteableBitmap(bitmap);
            wb.SaveJpeg(target, width, height, 0, 75);
            return target;
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            BitmapImage bitmapImage1 = new BitmapImage(new Uri("/SplashScreenImage.jpg", UriKind.RelativeOrAbsolute));
            bitmapImage1.CreateOptions = BitmapCreateOptions.None;
            byte[] data = ImageToByteArray(bitmapImage1);
            Image image1 = new Image();
            image1.Source=ByteArrayToBitmapImage(data);
            //stackPanel1.Children.Add(image1);
        }

    }
}
